<template>
  <div class="app-container">

    <!-- 搜索工作栏 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="表编号" prop="tableId">
        <el-input v-model="queryParams.tableId" placeholder="请输入表编号" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="字段名" prop="columnName">
        <el-input v-model="queryParams.columnName" placeholder="请输入字段名" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="字段类型" prop="dataType">
        <el-select v-model="queryParams.dataType" placeholder="请选择字段类型" clearable size="small">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="字段描述" prop="columnComment">
        <el-input v-model="queryParams.columnComment" placeholder="请输入字段描述" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="是否允许为空" prop="nullable">
        <el-select v-model="queryParams.nullable" placeholder="请选择是否允许为空" clearable size="small">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="是否主键" prop="primaryKey">
        <el-select v-model="queryParams.primaryKey" placeholder="请选择是否主键" clearable size="small">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="是否自增" prop="autoIncrement">
        <el-input v-model="queryParams.autoIncrement" placeholder="请输入是否自增" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="排序" prop="ordinalPosition">
        <el-input v-model="queryParams.ordinalPosition" placeholder="请输入排序" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="Java 属性类型" prop="javaType">
        <el-select v-model="queryParams.javaType" placeholder="请选择Java 属性类型" clearable size="small">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="Java 属性名" prop="javaField">
        <el-input v-model="queryParams.javaField" placeholder="请输入Java 属性名" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="字典类型" prop="dictType">
        <el-select v-model="queryParams.dictType" placeholder="请选择字典类型" clearable size="small">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="数据示例" prop="example">
        <el-input v-model="queryParams.example" placeholder="请输入数据示例" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="是否为 Create 创建操作的字段" prop="createOperation">
        <el-select v-model="queryParams.createOperation" placeholder="请选择是否为 Create 创建操作的字段" clearable size="small">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="是否为 Update 更新操作的字段" prop="updateOperation">
        <el-select v-model="queryParams.updateOperation" placeholder="请选择是否为 Update 更新操作的字段" clearable size="small">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="是否为 List 查询操作的字段" prop="listOperation">
        <el-select v-model="queryParams.listOperation" placeholder="请选择是否为 List 查询操作的字段" clearable size="small">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="List 查询操作的条件类型" prop="listOperationCondition">
        <el-input v-model="queryParams.listOperationCondition" placeholder="请输入List 查询操作的条件类型" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="是否为 List 查询操作的返回字段" prop="listOperationResult">
        <el-select v-model="queryParams.listOperationResult" placeholder="请选择是否为 List 查询操作的返回字段" clearable size="small">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="显示类型" prop="htmlType">
        <el-select v-model="queryParams.htmlType" placeholder="请选择显示类型" clearable size="small">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker v-model="queryParams.createTime" style="width: 240px" value-format="yyyy-MM-dd HH:mm:ss" type="daterange"
                        range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" :default-time="['00:00:00', '23:59:59']" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 操作工具栏 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                   v-hasPermi="['infra:info-column-collect:create']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" :loading="exportLoading"
                   v-hasPermi="['infra:info-column-collect:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 列表 -->
    <el-table v-loading="loading" :data="list">
      <el-table-column label="编号" align="center" prop="id" />
      <el-table-column label="表编号" align="center" prop="tableId" />
      <el-table-column label="字段名" align="center" prop="columnName" />
      <el-table-column label="字段类型" align="center" prop="dataType" />
      <el-table-column label="字段描述" align="center" prop="columnComment" />
      <el-table-column label="是否允许为空" align="center" prop="nullable" />
      <el-table-column label="是否主键" align="center" prop="primaryKey" />
      <el-table-column label="是否自增" align="center" prop="autoIncrement" />
      <el-table-column label="排序" align="center" prop="ordinalPosition" />
      <el-table-column label="Java 属性类型" align="center" prop="javaType" />
      <el-table-column label="Java 属性名" align="center" prop="javaField" />
      <el-table-column label="字典类型" align="center" prop="dictType" />
      <el-table-column label="数据示例" align="center" prop="example" />
      <el-table-column label="是否为 Create 创建操作的字段" align="center" prop="createOperation" />
      <el-table-column label="是否为 Update 更新操作的字段" align="center" prop="updateOperation" />
      <el-table-column label="是否为 List 查询操作的字段" align="center" prop="listOperation" />
      <el-table-column label="List 查询操作的条件类型" align="center" prop="listOperationCondition" />
      <el-table-column label="是否为 List 查询操作的返回字段" align="center" prop="listOperationResult" />
      <el-table-column label="显示类型" align="center" prop="htmlType" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template v-slot="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                     v-hasPermi="['infra:info-column-collect:update']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                     v-hasPermi="['infra:info-column-collect:delete']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize"
                @pagination="getList"/>

    <!-- 对话框(添加 / 修改) -->
    <el-dialog :title="title" :visible.sync="open" width="500px" v-dialogDrag append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="表编号" prop="tableId">
          <el-input v-model="form.tableId" placeholder="请输入表编号" />
        </el-form-item>
        <el-form-item label="字段名" prop="columnName">
          <el-input v-model="form.columnName" placeholder="请输入字段名" />
        </el-form-item>
        <el-form-item label="字段类型" prop="dataType">
          <el-select v-model="form.dataType" placeholder="请选择字段类型">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="字段描述" prop="columnComment">
          <el-input v-model="form.columnComment" placeholder="请输入字段描述" />
        </el-form-item>
        <el-form-item label="是否允许为空" prop="nullable">
          <el-radio-group v-model="form.nullable">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="是否主键" prop="primaryKey">
          <el-radio-group v-model="form.primaryKey">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="是否自增" prop="autoIncrement">
          <el-input v-model="form.autoIncrement" placeholder="请输入是否自增" />
        </el-form-item>
        <el-form-item label="排序" prop="ordinalPosition">
          <el-input v-model="form.ordinalPosition" placeholder="请输入排序" />
        </el-form-item>
        <el-form-item label="Java 属性类型" prop="javaType">
          <el-select v-model="form.javaType" placeholder="请选择Java 属性类型">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="Java 属性名" prop="javaField">
          <el-input v-model="form.javaField" placeholder="请输入Java 属性名" />
        </el-form-item>
        <el-form-item label="字典类型" prop="dictType">
          <el-select v-model="form.dictType" placeholder="请选择字典类型">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="数据示例" prop="example">
          <el-input v-model="form.example" placeholder="请输入数据示例" />
        </el-form-item>
        <el-form-item label="是否为 Create 创建操作的字段" prop="createOperation">
          <el-radio-group v-model="form.createOperation">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="是否为 Update 更新操作的字段" prop="updateOperation">
          <el-radio-group v-model="form.updateOperation">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="是否为 List 查询操作的字段" prop="listOperation">
          <el-radio-group v-model="form.listOperation">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="List 查询操作的条件类型" prop="listOperationCondition">
          <el-input v-model="form.listOperationCondition" placeholder="请输入List 查询操作的条件类型" />
        </el-form-item>
        <el-form-item label="是否为 List 查询操作的返回字段" prop="listOperationResult">
          <el-radio-group v-model="form.listOperationResult">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="显示类型" prop="htmlType">
          <el-select v-model="form.htmlType" placeholder="请选择显示类型">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { createInfoColumnCollect, updateInfoColumnCollect, deleteInfoColumnCollect, getInfoColumnCollect, getInfoColumnCollectPage, exportInfoColumnCollectExcel } from "@/api/infra/infoColumnCollect";

export default {
  name: "InfoColumnCollect",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 基础资料表元素汇总列表
      list: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        tableId: null,
        columnName: null,
        dataType: null,
        columnComment: null,
        nullable: null,
        primaryKey: null,
        autoIncrement: null,
        ordinalPosition: null,
        javaType: null,
        javaField: null,
        dictType: null,
        example: null,
        createOperation: null,
        updateOperation: null,
        listOperation: null,
        listOperationCondition: null,
        listOperationResult: null,
        htmlType: null,
        createTime: [],
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        tableId: [{ required: true, message: "表编号不能为空", trigger: "blur" }],
        columnName: [{ required: true, message: "字段名不能为空", trigger: "blur" }],
        dataType: [{ required: true, message: "字段类型不能为空", trigger: "change" }],
        columnComment: [{ required: true, message: "字段描述不能为空", trigger: "blur" }],
        nullable: [{ required: true, message: "是否允许为空不能为空", trigger: "blur" }],
        primaryKey: [{ required: true, message: "是否主键不能为空", trigger: "blur" }],
        autoIncrement: [{ required: true, message: "是否自增不能为空", trigger: "blur" }],
        ordinalPosition: [{ required: true, message: "排序不能为空", trigger: "blur" }],
        javaType: [{ required: true, message: "Java 属性类型不能为空", trigger: "change" }],
        javaField: [{ required: true, message: "Java 属性名不能为空", trigger: "blur" }],
        createOperation: [{ required: true, message: "是否为 Create 创建操作的字段不能为空", trigger: "blur" }],
        updateOperation: [{ required: true, message: "是否为 Update 更新操作的字段不能为空", trigger: "blur" }],
        listOperation: [{ required: true, message: "是否为 List 查询操作的字段不能为空", trigger: "blur" }],
        listOperationCondition: [{ required: true, message: "List 查询操作的条件类型不能为空", trigger: "blur" }],
        listOperationResult: [{ required: true, message: "是否为 List 查询操作的返回字段不能为空", trigger: "blur" }],
        htmlType: [{ required: true, message: "显示类型不能为空", trigger: "change" }],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      // 执行查询
      getInfoColumnCollectPage(this.queryParams).then(response => {
        this.list = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    /** 取消按钮 */
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 表单重置 */
    reset() {
      this.form = {
        id: undefined,
        tableId: undefined,
        columnName: undefined,
        dataType: undefined,
        columnComment: undefined,
        nullable: undefined,
        primaryKey: undefined,
        autoIncrement: undefined,
        ordinalPosition: undefined,
        javaType: undefined,
        javaField: undefined,
        dictType: undefined,
        example: undefined,
        createOperation: undefined,
        updateOperation: undefined,
        listOperation: undefined,
        listOperationCondition: undefined,
        listOperationResult: undefined,
        htmlType: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加基础资料表元素汇总";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id;
      getInfoColumnCollect(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改基础资料表元素汇总";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (!valid) {
          return;
        }
        // 修改的提交
        if (this.form.id != null) {
          updateInfoColumnCollect(this.form).then(response => {
            this.$modal.msgSuccess("修改成功");
            this.open = false;
            this.getList();
          });
          return;
        }
        // 添加的提交
        createInfoColumnCollect(this.form).then(response => {
          this.$modal.msgSuccess("新增成功");
          this.open = false;
          this.getList();
        });
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id;
      this.$modal.confirm('是否确认删除基础资料表元素汇总编号为"' + id + '"的数据项?').then(function() {
          return deleteInfoColumnCollect(id);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      // 处理查询参数
      let params = {...this.queryParams};
      params.pageNo = undefined;
      params.pageSize = undefined;
      this.$modal.confirm('是否确认导出所有基础资料表元素汇总数据项?').then(() => {
          this.exportLoading = true;
          return exportInfoColumnCollectExcel(params);
        }).then(response => {
          this.$download.excel(response, '基础资料表元素汇总.xls');
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
